package ar.posti.framework.query.core;

import java.util.Iterator;
import java.util.List;

import ar.posti.framework.query.Query;

public abstract class AbstractQuerySupport<Q> implements Query {
	private QueryExecutor<Q> queryExecutor;

	public List list() {
		Q query = assembleQuery();
		return queryExecutor.queryForList(query);
	}

	public Iterator iterate() {
		Q query = assembleQuery();
		return queryExecutor.queryForIterator(query);
	}

	public Iterator iterate(Integer firstElement, Integer lastElement) {
		Q query = assembleQuery(firstElement, lastElement);
		return queryExecutor.queryForIterator(query);
	}
	public int count() {
		Q query = assembleCountQuery();
		return queryExecutor.queryForInt(query);
	}

	public List list(Integer firstElement, Integer lastElement) {
		Q query = assembleQuery(firstElement, lastElement);
		return queryExecutor.queryForList(query);
	}

	public Object uniqueResult() {
		Q query = assembleQuery();
		return queryExecutor.queryForObject(query);
	}
	
	public void setQueryExecutor(QueryExecutor queryExecutor) {
		this.queryExecutor = queryExecutor;
	}

	protected abstract Q assembleCountQuery();
	protected abstract Q assembleQuery();
	protected abstract Q assembleQuery(Integer firstElement, Integer lastElement);
}